package com.customization.avic;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.concurrent.TimeUnit;

/**
 * @author dusongsong
 * @date 2023年12月07日 10:23
 */
public class DoneWfUtil {

    private static BaseBean baseBean = new BaseBean();
    private static final String URL = "http://uat.fsyoa.fsygroup.com/rest/ofs/deleteUserRequestInfoByJson";

    public static JSONObject sendDoneWorkflow(String requestid,String userid) {
        JSONObject paramsJson = new JSONObject();
        paramsJson.put("syscode","OA");
        paramsJson.put("flowid",requestid);
        paramsJson.put("userid",userid);
        baseBean.writeLog(">>>>>sendDoneWorkflow>>>>paramsJson="+paramsJson);
        return sendOKHttpRequest(paramsJson);
    }

    //调用接口
    public static JSONObject sendOKHttpRequest(JSONObject params) {
        JSONObject resultJson = new JSONObject();
        String code = "failure";
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params.toJSONString());
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

}
